﻿using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using Libraries.Common;
using Libraries.Entities;

namespace Libraries.Repositories
{
    public class CourseCategoryRepository
    {
        private CourseCategory ParseToEntityCourse(IDataRecord dataReader)
        {
            if (dataReader != null)
            {
                return new CourseCategory
                {
                    CourseCategoryId = dataReader["CourseCategoryId"].ToInt32OrDefault(),
                    ParentCategory = dataReader["ParentCategory"].ToStringOrDefault(),
                    Category = dataReader["Category"].ToStringOrDefault(),
                    ParentCourseCategoryId = dataReader["ParentCourseCategoryId"].ToInt32OrDefault()
                };
            }
            return null;
        }

        public Collection<CourseCategory> CourseCategorySearchPaging(string courseCategoryName)
        {
            var parameters = new Collection<SqlParameter>
                                                      {
                                                          new SqlParameter("@CourseCategoryName",courseCategoryName),
                                                      };

            Collection<CourseCategory> list = null;
            if (!string.IsNullOrEmpty(ProcedureConfiguration.CourseCategorySearchPaging))
            {
                using (var result = RepositorySql.SqlHelper.ExecuteReader(ProcedureConfiguration.CourseCategorySearchPaging, parameters, ConnectionConfiguration.ConnectionString))
                {
                    if (result != null)
                    {
                        list = new Collection<CourseCategory>();
                        while (result.Read())
                        {
                            list.Add(ParseToEntityCourse(result));
                        }
                    }
                }
            }
            return list;
        }

        public Collection<CourseCategory> CourseCategoryFetchAllName()
        {
         
            Collection<CourseCategory> list = null;
            if (!string.IsNullOrEmpty(ProcedureConfiguration.CourseCategoryFetchAllName))
            {
                using (var result = RepositorySql.SqlHelper.ExecuteReader(ProcedureConfiguration.CourseCategoryFetchAllName, ConnectionConfiguration.ConnectionString))
                {
                    if (result != null)
                    {
                        list = new Collection<CourseCategory>();
                        while (result.Read())
                        {
                            list.Add(ParseToEntityCourse(result));
                        }
                    }
                }
            }
            return list;
        }
    }
}